package com.ruoyi.scale.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.scale.document.ScaleLog;
import com.ruoyi.scale.service.ScaleLogService;
import com.ruoyi.scale.vo.AnswerScaleVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("/scale/log")
public class ScaleLogController extends BaseController {
    @Resource
    private ScaleLogService scaleLogService;

    @PreAuthorize("@ss.hasPermi('*:*:*')")
    @GetMapping(value = "/result/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") String logId) {
        return success(scaleLogService.getById(logId).orElse(null));
    }


    @PostMapping(value = "/answer")
    @Anonymous
    public AjaxResult answer(@RequestBody @Validated AnswerScaleVo answerScaleVo){
        return success(scaleLogService.answer(answerScaleVo));
    }

    @GetMapping("getLog/{scaleLogId}")
    @Anonymous
    public AjaxResult getLog(@PathVariable String scaleLogId){
        Optional<ScaleLog> scaleLogOpt = scaleLogService.getById(scaleLogId);
        if (scaleLogOpt.isPresent()) {
            return success(scaleLogOpt.get());
        }
        return error("无效的参数!");
    }
}
